package com.kotle.baselib.retfofit;

import android.content.Context;
import android.net.ParseException;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.io.EOFException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.observers.DisposableObserver;
import retrofit2.HttpException;


/**
 * Created by Kotle on 2017/3/28.
 * 观察者对象
 */

public class BaseSubscriberResultNoLimit<T, P> extends DisposableObserver<T> {
    private NetRequestCall<T, P> call;
    private Context context;
    //对应HTTP的状态码
    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;
    //出错提示
    private final String networkMsg = "";
    private final String parseMsg = "";
    private final String unknownMsg = "";

    public BaseSubscriberResultNoLimit(NetRequestCall<T, P> call, Context context) {
        this.call = call;
        this.context = context;
    }

    @Override
    public void onStart() {
        if (call != null) {
            call.onStart();
        }
    }

    @Override
    public void onNext(T t) {
        if (call != null) {
            call.onNext(t);
        }
    }

    @Override
    public void onError(Throwable e) {
        if (call != null) {
            call.onError(e);
        }
        exceptionHandling(e);
    }

    @Override
    public void onComplete() {
        if (call != null) {
            call.onCompleted();
        }
    }

    private void exceptionHandling(Throwable e) {
        Throwable throwable = e;
        //获取最根源的异常
        while (throwable.getCause() != null) {
            e = throwable;
            throwable = throwable.getCause();
        }
        if (e instanceof HttpException) {             //HTTP错误
            HttpException httpException = (HttpException) e;
            switch (httpException.code()) {
                case UNAUTHORIZED://非法
                    showToast("访问未授权401");
                    break;
                case FORBIDDEN://权限拒绝
                    showToast("访问被拒绝403");
                    break;
                case NOT_FOUND://未找到
                    showToast("访问未找到404");
                    break;
                case REQUEST_TIMEOUT://请求超时
                    showToast("请求超时408");
                    break;
                case GATEWAY_TIMEOUT://网管超时
                    showToast("网关超时504");
                    break;
                case INTERNAL_SERVER_ERROR://内部服务错误
                    showToast("内部服务器错误500");
                    break;
                case BAD_GATEWAY://网管无效
                    showToast("无效网关502");
                    break;
                case SERVICE_UNAVAILABLE://找不到服务器
                    showToast("找不到服务器503");
                    break;
                default:
                    showToast(e.getMessage());
                    break;
            }
            try {
                Log.e("HttpErrorMsg:------>", ((HttpException) e).response().errorBody().string());
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            showToast("数据解析错误");
        } else if (e instanceof NullPointerException) {
            showToast("空指针错误");
        } else if (e instanceof UnknownHostException) {
            showToast("网络连接已中断");
        } else if (e instanceof SocketTimeoutException) {
            showToast("连接超时");
        } else if (e instanceof EOFException) {
            showToast("操作失败，请重试");
        } else {
            showToast(e.getMessage());
        }
    }

    private void showToast(String msg) {
        if (context != null) {
            Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
        }
    }
}
